package com.sjlh.centurylianhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.Inventory;
import com.sjlh.centurylianhua.entity.LossesLog;
import com.sjlh.centurylianhua.mapper.InventoryMapper;
import com.sjlh.centurylianhua.mapper.LosseslogMapper;
import com.sjlh.centurylianhua.utils.ResultData;
import com.sjlh.centurylianhua.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/losseslog")
@CrossOrigin
public class LosseslogController {

    @Autowired
    private LosseslogMapper losses;
    @Autowired
    private InventoryMapper inventoryMapper;

    @RequestMapping("/all")
    public ResultData getAll(@RequestBody SearchCon sc){
//      分页对象，传入当前页码及每页的数量
        QueryWrapper<LossesLog> wra=new QueryWrapper<LossesLog>();
        wra.like(StringUtils.isnotNull(sc.getName()),"user.userName",sc.getName());
        wra.eq(sc.getTypeName2()!=null,"lo.depotId",sc.getTypeName2());
        wra.eq(sc.getTypeName3()!=null,"lo.productId",sc.getTypeName3());
        wra.ge(StringUtils.isnotNull(sc.getBeginDate()),"lo.createTime",sc.getBeginDate());
        wra.le(StringUtils.isnotNull(sc.getEndDate()),"lo.createTime",sc.getEndDate());
        wra.apply("user.id = lo.userId and product.id=lo.productId and depot.id=lo.depotId");
        //封装分页对象
        if(sc.getPage()!=null) {
            Page<LossesLog> pa=new Page<LossesLog>(sc.getPage(),sc.getLimit());
            List<LossesLog> uspa=losses.loselist2(pa,wra);
            return new ResultData(true,20000," " ,uspa,pa.getTotal());
        }else{
            long count= losses.loselist1( wra).size();
            return new ResultData(true,20000," " ,losses.loselist1(wra),count);
        }
    }
    @RequestMapping("/add")
    public ResultData add(@RequestBody LossesLog loss){
        if(loss!=null&& !"".equals(loss) &&loss.getUserId()!=null){//添加报损日志
            QueryWrapper<Inventory> wra=new QueryWrapper<Inventory>();
            wra.eq(loss.getDepotId()!=null,"depotId",loss.getDepotId());
            wra.eq(loss.getProductId()!=null,"productId",loss.getProductId());
            Inventory ins=inventoryMapper.selectOne(wra);
            if(ins.getDepotNum()-loss.getNum()>=0){//库存数量必须大于0
                int r=losses.insert(loss);//判断为true时新增报溢数据
                UpdateWrapper<Inventory> updateWrapper = new UpdateWrapper();
                updateWrapper.setSql("depotNum = depotNum - " +loss.getNum());//进货数量
                updateWrapper.eq("productId",loss.getProductId());//商品id
                updateWrapper.eq("depotId",loss.getDepotId());//仓库id
                inventoryMapper.update(null,updateWrapper);//修改库存表
                return new ResultData(true, 20000, "数据提交成功!", true, null);
            }else{
                return new ResultData(true, 20000, "库存数量不足!剩余:"+ins.getDepotNum()+"件", false, null);
            }
        }else{
            return new ResultData(false,20000,"数据不能为空!" ,false,null);
        }
    }
}
